package com.moduls.interfaceFunction.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.aliyuncs.exceptions.ClientException;
import com.moduls.interfaceFunction.alipay.config.AlipayConfig;
import com.moduls.interfaceFunction.core.HtmlUtils;
import com.moduls.interfaceFunction.core.Result;
import com.moduls.interfaceFunction.core.SqlMapper;
import com.moduls.interfaceFunction.message.MessageController;
import com.moduls.interfaceFunction.model.Systemmessage;
import com.moduls.interfaceFunction.weixin.pay.WeixinPay;
import com.moduls.push.UPushUtils;
import com.mysql.fabric.xmlrpc.base.Array;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

//供应商端接口
public class HxSupplyApi extends HxTBackApi {

	//接口开始*******************************************************************
	
	// 接口功能：注册供应商 sms_code（验证码）：1111
	public void s_register_supply_set() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String phone =  obj_vars.optString("phone"); // 账号（手机号）
		String pwd =  obj_vars.optString("pwd"); // 密码
		String sms_id =  obj_vars.optString("sms_id"); // 验证码id
		String sms_code =  obj_vars.optString("sms_code"); // 验证码
		String contact_name =  obj_vars.optString("contact_name"); //联系人
		String contact_phone =  obj_vars.optString("contact_phone"); //联系电话
		String company =  obj_vars.optString("company"); //公司
		String address =  obj_vars.optString("address"); // 地址
		Map<String, Object> code= sqlMapper.public_one_detail(String.format("select sms_code from p_sms where sms_id=%s and sms_phone='%s' and sms_code='%s' ", sms_id, phone, sms_code));
		if(code == null) {
			results.setErrorMessage("验证码不正确");
		}else {
			if (sqlMapper.public_one_scale(String.format("select uid from p_user where utype=%s and phone='%s' ", 2, phone)) != null) {
				results.setErrorMessage("该手机号已被注册");
			}
			
			//注册或重新注册用户信息
			int uid = this.m_register_user(2, phone, pwd);
			
			Map<String, String> map =  new HashMap<String, String>();
			map.put("sid", String.valueOf(uid));
			map.put("contact_name", contact_name);
			map.put("contact_phone", contact_phone);
			map.put("company", company);
			map.put("address", address);
			int sid =  sqlMapper.insert_or_update("p_supply", map, null);
			
			results.setMessage("提交成功，请等待审核");//输出：提示信息
			results.setStatus(1);//输出：状态1成功 0失败
		}
	}
		
	// 接口功能：供应商登录系统，
	public void s_login_supply_get() throws IOException, SQLException {
		JSONObject obj_vars =  getObj_vars();
		String phone =  obj_vars.optString("phone"); // 手机号
		String pwd =  obj_vars.optString("pwd"); // 密码
		String device_tokens =  obj_vars.optString("device_tokens"); // 苹果或安卓的设备号
		
		pwd = basictool.MD5(pwd);
		Map<String, Object> mapData = this.m_supply_login_get(phone, pwd, device_tokens);
		results.setStatus(1);//输出：状态1成功 0失败
		results.setResult(mapData);//输出：如果返回approve_status为1账户正在审核中，弹出提示对话框；为3账户被驳回，要跳转到完善信息页面，用户重新填写信息再次提交；为2正常登陆
		
	}
	
	//供应商登陆:账号密码登陆和验证码登陆
	private Map<String, Object> m_supply_login_get(String phone, String pwd, String device_tokens) throws IOException, SQLException
	{
		String swhere_pwd = "";
		if(pwd != null)
		{
			swhere_pwd = String.format(" and pu.pwd='%s' ", pwd);
		}
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		Map<String, Object> mapData= sqlMapper.public_one_detail(String.format("select pu.uid, pu.phone, f_name(pu.nick_name,pu.phone) as nick_name,f_img(pu.avatar,1) as avatar,pu.token,pu.approve_status,ss.contact_name,ss.contact_phone,ss.company,ss.address,ha.approve_opinion,pu.is_forbid  "
				+ "from p_user pu join p_supply ss on pu.uid=ss.sid left join hx_approve ha on pu.uid=ha.ref_id and ha.approve_type=100 and ha.is_last=1 where pu.utype=2 and pu.phone='%s' %s ", phone, swhere_pwd));
		if(mapData == null) {
			results.setErrorMessage("账号或密码不正确");
		}else {
			//注册或重新注册用户信息
			String approve_status = mapData.get("approve_status").toString();
			String is_forbid = mapData.get("is_forbid").toString();
			if(approve_status.equals("1"))
			{
				results.setMessage("您的账户正在审核中，请耐心等待");
			}
			else if(approve_status.equals("3"))
			{
				if(mapData.get("approve_opinion").toString().isEmpty())
				{
					results.setMessage("您的账户申请已驳回，请重新填写");
				}
				else
				{
					results.setMessage("您的账户申请已驳回，请重新填写，驳回原因: " + mapData.get("approve_opinion").toString());
				}
			}
			else if(is_forbid.equals("1"))
			{
				results.setMessage("您的账户已禁用");
			}
			else//正常登陆
			{
				String token = UUID.randomUUID().toString();
				String last_login_time = this.current_time();
				Map<String,String> map = new HashMap<String,String>();
				map.put("token", token);
				map.put("last_login_time", last_login_time);
				if(device_tokens != null && !device_tokens.isEmpty())
				{
					map.put("device_tokens", device_tokens);
				}
				sqlMapper.insert_or_update("p_user", map, "uid="+mapData.get("uid").toString());
				
				//更新返回值result中的token和最后登录时间
				mapData.put("token", token);
			}
		}
		
		return mapData;
	}
	
	// 接口功能：验证码登陆
	public void s_sms_login_get() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String phone =  obj_vars.optString("phone"); // 手机号
		String sms_id =  obj_vars.optString("sms_id"); // 验证码id
		String sms_code =  obj_vars.optString("sms_code"); // 验证码
		String device_tokens =  obj_vars.optString("device_tokens"); // 苹果或安卓的设备号
		
		Map<String, Object> code= sqlMapper.public_one_detail(String.format("select sms_code from p_sms where sms_id=%s and sms_phone='%s' and sms_code='%s' ", sms_id, phone, sms_code));
		if(code == null) {
			results.setErrorMessage("验证码不正确");
		}else {
			Map<String, Object> mapData = this.m_supply_login_get(phone, null, device_tokens);
			results.setStatus(1);//输出：状态1成功 0失败
			results.setResult(mapData);//输出：如果返回approve_status为1账户正在审核中，弹出提示对话框；为3账户被驳回，要跳转到完善信息页面，用户重新填写信息再次提交；为2正常登陆
		}
	}
		
	// 接口功能：注册驳回后重新提交供应商信息 sms_code（验证码）：1111
	public void s_resubmit_supply_set() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		int uid = Integer.parseInt(obj_vars.optString("uid").toString()); // 用户id
		String contact_name =  obj_vars.optString("contact_name"); //联系人
		String contact_phone =  obj_vars.optString("contact_phone"); //联系电话
		String company =  obj_vars.optString("company"); //公司
		String address =  obj_vars.optString("address"); // 地址
		
		String approve_status = sqlMapper.public_one_scale("select approve_status from p_user where uid=" + uid);
		if(approve_status == null)
		{
			results.setErrorMessage("账户审核状态不正确");
		}
		else if(approve_status.equals("1"))
		{
			results.setErrorMessage("账户在审核中，不允许修改");
		}
		else if(approve_status.equals("2"))
		{
			results.setErrorMessage("账户已审核通过，不允许修改");
		}

		Map<String, String> map =  new HashMap<String, String>();
		map.put("contact_name", contact_name);
		map.put("contact_phone", contact_phone);
		map.put("company", company);
		map.put("address", address);
		sqlMapper.insert_or_update("p_supply", map, "sid="+uid);
		
		//更新为待审核状态
		sqlMapper.update(String.format("update p_user set approve_status=1 where uid=%s ", uid));
		
		results.setMessage("提交成功，请等待审核");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	
	// 接口功能: 获取供应商订单
	//vars={"token":"8401361e-6224-44b1-b583-0bf0075066a4","uid":"12","action":"s_suppply_order_get","type":"待配送","pagenum":"0","pagesize":"0"}
	public void s_suppply_order_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String sid = obj_vars.optString("uid");//供应商id
		String type = obj_vars.optString("type");//订单类型(待配送,待收货，已完成)
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		//查询订单list
		String sqllist = "";
		switch (type) {
		case "待配送":
			sqllist = " select po.oid,po.cid,pod.detail_oid,po.contact_detail, po.address_detail,"
					+ " DATE_FORMAT(po.send_time,'%Y-%m-%d') time,poi.order_item_id, poi.item_name,poi.val_names,poi.actual_num,"
					+ " if(po.querensd_time is NULL,0,0) is_have,po.status  "
					+ " from p_order po "
					+ " inner join p_order_detail pod on po.oid = pod.oid "
					+ " inner join p_order_item poi on pod.detail_oid = poi.detail_oid "
					+ " where po.status = '待配送' and poi.source_type = 2 and pod.order_type = 1 "
					+ " and poi.source_id = "+sid+" and po.is_history = 0 and pod.order_type = 1 order by po.send_time asc,poi.create_time asc  ";
			break;
		case "待收货":
			sqllist = " select po.oid,po.cid,pod.detail_oid,po.contact_detail, po.address_detail,"
					+ " DATE_FORMAT(po.send_time,'%Y-%m-%d') time,poi.order_item_id, poi.item_name,poi.val_names,poi.actual_num,"
					+ " if(po.querensd_time is NULL,1,2) is_have,po.status  "
					+ " from p_order po "
					+ " inner join p_order_detail pod on po.oid = pod.oid "
					+ " inner join p_order_item poi on pod.detail_oid = poi.detail_oid "
					+ " where po.status = '待收货' and poi.source_type = 2 and pod.order_type = 1 "
					+ " and poi.source_id = "+sid+" and po.is_history = 0 and pod.order_type = 1 order by po.send_time asc,poi.create_time asc  ";
			break;
		case "已完成":
			sqllist = " select po.oid,po.cid,pod.detail_oid,po.contact_detail, po.address_detail,"
					+ " DATE_FORMAT(po.send_time,'%Y-%m-%d') time,poi.order_item_id, poi.item_name,poi.val_names,poi.actual_num,"
					+ " if(po.querensd_time is NULL,0,0) is_have,po.status  "
					+ " from p_order po "
					+ " inner join p_order_detail pod on po.oid = pod.oid "
					+ " inner join p_order_item poi on pod.detail_oid = poi.detail_oid "
					+ " where po.status = '已完成' and poi.source_type = 2 and pod.order_type = 1 "
					+ " and poi.source_id = "+sid+" and po.is_history = 0 and pod.order_type = 1 order by po.send_time asc,poi.create_time asc  ";
			break;
		default:
			break;
		}
		//source_type  供货来源类型：1库存；2供应商；3临采师傅   //is_history  是否历史订单：0正常；1用户删除；2后台删除 //order_type  订单类型：1真实订单；2虚拟订单
		List<Map<String, Object>> lstItems = sqlMapper.public_list_page_sql(sqllist, pagenum,pagesize);
		
		List<Map<String,Object>> lstMapOrder = new ArrayList<Map<String,Object>>();
		Map<String,Object> mapOrder = null;
		List<Map<String,Object>> lstMapitems = null;
		String oid = "";
		for(Map<String,Object> mapItem: lstItems)
		{
			if(oid.isEmpty() || !oid.equals(mapItem.get("oid").toString()))
			{
				mapOrder = new HashMap<String,Object>();
				oid = mapItem.get("oid").toString();
				mapOrder.put("oid", mapItem.get("oid"));
				mapOrder.put("cid", mapItem.get("cid"));
				mapOrder.put("detail_oid", mapItem.get("detail_oid"));
				mapOrder.put("contact_detail", mapItem.get("contact_detail"));
				mapOrder.put("address_detail", mapItem.get("address_detail"));
				mapOrder.put("time", mapItem.get("time"));
				mapOrder.put("is_have", mapItem.get("is_have"));
				mapOrder.put("status", mapItem.get("status"));
				//订单数据
				lstMapOrder.add(mapOrder);
				//订单里面的商品
				lstMapitems = new ArrayList<Map<String,Object>>();
				mapOrder.put("items", lstMapitems);
			}
			
			Map<String,Object> mapOrderItem = new HashMap<String,Object>(); 
			mapOrderItem.put("order_item_id", mapItem.get("order_item_id"));
			mapOrderItem.put("item_name", mapItem.get("item_name"));
			mapOrderItem.put("val_names", mapItem.get("val_names"));
			mapOrderItem.put("actual_num", mapItem.get("actual_num"));
			lstMapitems.add(mapOrderItem);
		}
		
		
		results.setResult(lstMapOrder);//输出：供应商待配送订单   待配送订单有 is_hava 1:未确认送达，2：已确认送达
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:订单详情
	//vars={"token":"8401361e-6224-44b1-b583-0bf0075066a4","uid":"4","action":"s_pending_distribution_order_details_pei_get","oid":"2"}
	public void s_pending_distribution_order_details_pei_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars = getObj_vars();
		String oid = obj_vars.optString("oid");//订单id
		
		String type = sqlMapper.public_one_scale("select status from p_order where oid = "+oid+"");
		//查询订单详情 map
		String sqlmap = "select po.oid,po.order_note,po.cid,pod.detail_oid,po.status,po.contact_detail, po.address_detail,po.order_no, ";
		switch (type) {
		case "待配送":
			sqlmap+="  DATE_FORMAT(po.send_time,'%Y-%m-%d') time,"
				   + " DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') create_time,"
				   + " if(po.querensd_time is NULL,0,0) is_have ";
			break;
		case "待收货":
			sqlmap+="  IFNULL(DATE_FORMAT(po.querensd_time,'%Y-%m-%d'), DATE_FORMAT(po.send_time,'%Y-%m-%d')) time,"
				   + " DATE_FORMAT(po.querenps_time,'%Y-%m-%d') querenps_time,"
				   + " DATE_FORMAT(po.querensd_time,'%Y-%m-%d') querensd_time,"
				   + " DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') create_time, "
				   + " if(po.querensd_time is NULL,1,2) is_have ";
			break;
		case "已完成":
			sqlmap+="   DATE_FORMAT(po.receive_time,'%Y-%m-%d') time, "
					+ " DATE_FORMAT(po.querenps_time,'%Y-%m-%d') querenps_time,"
					+ " DATE_FORMAT(po.querensd_time,'%Y-%m-%d') querensd_time,"
					+ " DATE_FORMAT(po.receive_time,'%Y-%m-%d') receive_time,"
					+ " DATE_FORMAT(po.create_time,'%Y-%m-%d %H:%i:%m') create_time, "
					+ " if(po.querensd_time is NULL,0,0) is_have ";
			break;
		}
		sqlmap+="from p_order po inner join p_order_detail pod on po.oid = pod.oid inner join p_customer pc on po.cid = pc.cid where pod.order_type = 1 and po.oid = "+oid+" ";
		Map<String, Object> map = sqlMapper.public_one_detail(sqlmap);
		//查询订单商品 item 商品信息列表
		String detail_oid = map.get("detail_oid").toString();//订单详情id
		String sqlitemlist = "select poi.order_item_id, poi.item_name,ps.val_names,poi.actual_num,poi.customer_note from p_order_item poi inner join p_sku ps on poi.sku_id = ps.sku_id where poi.detail_oid = %s order by poi.create_time asc";
		List<Map<String, Object>> itemList = sqlMapper.public_list_all_sql(String.format(sqlitemlist, detail_oid));
		map.put("item", itemList);
		 
		results.setResult(map);//输出：订单详情 querenps_time 确认配送时间  querensd_time确认送达时间  receive_time 用户确认收货时间
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:确认配送按钮
	//vars={"token":"08f56aa2-6e45-4999-b38c-80495fe54b84","uid":"12","action":"s_distribution_set","oid":"2"}
	public void s_distribution_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars = getObj_vars();
		String oid = obj_vars.optString("oid");//订单id
		
		//修改订单状态
		String sql = "update p_order po set status = '待收货', querenps_time = now() where po.oid = %s";
		sqlMapper.update(String.format(sql, oid)); //更新状态
		
		results.setMessage("确认成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:确认送达按钮
	//vars={"token":"08f56aa2-6e45-4999-b38c-80495fe54b84","uid":"12","action":"s_delivery_set","oid":"2","itemlist":[{"order_item_id":"2","num":"10"},{"order_item_id":"3","num":"20"}]}
	public void s_delivery_set() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars = getObj_vars();//获取入参
		int oid = obj_vars.optInt("oid");//订单id
		String sid = obj_vars.optString("uid");//确认人id
		String itemlist = obj_vars.optString("itemlist");//需要修改的商品信息  "itemlist":[{"order_item_id":"123","num":"10"},{"order_item_id":"234","num":"20"}]
		
		//确认送达 
		String sql = "update p_order set querensd_time = now() where oid = %s ";
		sqlMapper.update(String.format(sql, oid));
		//修改商品数量信息
		if (!itemlist.equals("")) {
			List<Map<String, Object>> ilist = JSONArray.toList(JSONArray.fromObject(itemlist), Map.class);
			for (Map<String, Object> map : ilist) {
				String sqlup = "update p_order_item set actual_num = %s where order_item_id = %s";
				sqlMapper.update(String.format(sqlup, map.get("num"), map.get("order_item_id")));
			}
		}
		
		results.setMessage("确认成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	
	//接口功能:我的(供应商)
	//vars={"token":"08f56aa2-6e45-4999-b38c-80495fe54b84","uid":"12","action":"s_my_get"}
	public void s_my_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars = getObj_vars();//获取入参
		String uid = obj_vars.optString("uid");//供应商登录者uid
		
		String sql = "select pu.uid,f_name(pu.nick_name, pu.phone) as nick_name , f_img(pu.avatar,1) as avatar,ps.company,pu.phone from p_user pu inner join p_supply ps on pu.uid = ps.sid where pu.uid = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, uid));
		
		results.setResult(map);//输出：我的信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:修改绑定手机号
	//vars={"token":"08f56aa2-6e45-4999-b38c-80495fe54b84","uid":"12","action":"s_update_phone_set","phone":"17607186989","sms_id":"45","sms_code":"1234"}
	public void s_update_phone_set() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String phone =  obj_vars.optString("phone"); // 手机号
		String sms_id =  obj_vars.optString("sms_id"); // 验证码id
		String sms_code =  obj_vars.optString("sms_code"); // 验证码
		String uid = obj_vars.optString("uid");//供应商uid
		
		Map<String, Object> code= sqlMapper.public_one_detail(String.format("select sms_code from p_sms where sms_id=%s and sms_phone= %s and sms_code= %s ", sms_id, phone, sms_code));
		if(code == null) {
			results.setErrorMessage("验证码不正确");
		}else {
			if (sqlMapper.public_one_scale(String.format("select uid from p_user where phone= %s ", phone)) != null) {
				results.setErrorMessage("该手机号已被注册");
			} else {
				//更换手机号
				String sqlupdate = "update p_user set phone = %s where uid = %s";
				sqlMapper.update(String.format(sqlupdate, phone,uid)); //更换手机号
				results.setMessage("换绑成功");
			}
		}
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:修改密码
	//vars={"token":"08f56aa2-6e45-4999-b38c-80495fe54b84","uid":"12","action":"s_update_pwd_set","old_pwd":"123456","new_pwd":"123456"}
	public void s_update_pwd_set() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String uid = obj_vars.optString("uid"); //供应商id
		String old_pwd =  obj_vars.optString("old_pwd"); // 旧密码
		String new_pwd =  obj_vars.optString("new_pwd"); // 新密码
		
		//验证旧密码是否正确
		String sqlpwd = "select pu.pwd from p_user pu where pu.uid = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sqlpwd, uid));
		if (map.get("pwd").equals(basictool.MD5(old_pwd))) {
			//更新密码
			String sqlupdate = "update p_user set pwd = '%s' where uid = %s";
			sqlMapper.update(String.format(sqlupdate, basictool.MD5(new_pwd),uid));
			
			results.setMessage("修改成功");
		} else {
			results.setErrorMessage("旧密码错误");
		}
		
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:查询个人资料
	//vars={"token":"08f56aa2-6e45-4999-b38c-80495fe54b84","uid":"12","action":"s_personal_data_get"}
	public void s_personal_data_get() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String uid = obj_vars.optString("uid"); //供应商id
		
		//查询个人资料
		String sql = "select f_name(pu.nick_name, pu.phone) as nick_name ,f_img(pu.avatar,1) as avatar, pu.avatar as avatars,pu.sex,pu.phone,ps.company from p_user pu inner join p_supply ps on pu.uid = ps.sid where pu.uid = %s";
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sql, uid));
		
		results.setResult(map);//输出：个人资料信息 avatars:没有前缀头像
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
	}
	
	//接口功能:更新个人资料
	//vars={"token":"08f56aa2-6e45-4999-b38c-80495fe54b84","uid":"12","action":"s_personal_data_set","avatar","imgage/ier.jpg":"nick_name":"春天里","sex":"1"}
	public void s_personal_data_set() throws IOException, SQLException {
		SqlMapper sqlMapper= new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String uid = obj_vars.optString("uid"); //供应商id
		String avatar = obj_vars.optString("avatar"); //头像地址
		String nick_name = obj_vars.optString("nick_name"); //昵称
		String sex = obj_vars.optString("sex"); //性别
		
		//修改个人资料
		String sql = "update p_user set avatar = '%s', nick_name  = '%s', sex = %s where uid = %s";
		sqlMapper.update(String.format(sql, avatar,nick_name,sex,uid));
		
		results.setMessage("更新成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	//接口功能:首页接口
	//vars={"token":"9ca993c9-6199-4426-8113-f71cfe3ecf47","uid":"101","action":"s_home_page_get"}
	public void s_home_page_get() throws IOException, SQLException {
		SqlMapper sqlMapper = new SqlMapper(sqlSessions);
		JSONObject obj_vars =  getObj_vars();
		String uid = obj_vars.optString("uid");//客户或商家id
		
		int pagenum = obj_vars.optInt("pagenum");//当前页数x
		int pagesize = obj_vars.optInt("pagesize");//每页条数 (给0 默认为12)
		
		//订单消息
		String sqlmsg = "select pm.msg_id,pmd.msg_content from p_message pm "
				+ " inner join p_message_define pmd on pm.msg_define_id = pmd.msg_define_id "
				+ " where uid = %s and pm.is_history = 0 and pmd.msg_type = 400 order by pm.create_time desc";
		
		List<Map<String, Object>> list = sqlMapper.public_list_all_sql(String.format(sqlmsg, uid));
		
		//未读消息数量
		String sqlunmsg = "select count(1) nums "
				+ " from p_message_define pm left join p_message pd on pm.msg_define_id=pd.msg_define_id and pd.uid=%s "
				+ " where pd.msg_id is null and pm.msg_type in (400,500) and is_history = 0 ";
		
		Map<String, Object> map = sqlMapper.public_one_detail(String.format(sqlunmsg, uid));
		
		map.put("list", list);
		
		//查询订单list
		//source_type  供货来源类型：1库存；2供应商；3临采师傅   //is_history  是否历史订单：0正常；1用户删除；2后台删除 //order_type  订单类型：1真实订单；2虚拟订单
		String sqllist = "select po.oid,po.cid,pod.detail_oid,po.contact_detail, po.address_detail,"
				+ "DATE_FORMAT(po.send_time,'%Y-%m-%d %H:%i') time,poi.order_item_id, poi.item_name,poi.val_names,poi.actual_num  "
				+ "from p_order po "
				+ " inner join p_order_detail pod on po.oid = pod.oid "
				+ " left join p_customer pc on po.cid = pc.cid "
				+ " inner join p_order_item poi on pod.detail_oid = poi.detail_oid "
				+ "where po.status = '"+"待配送"+"' and poi.source_type = 2 and poi.source_id = "+uid+" "
				+ "and po.is_history = 0 and pod.order_type = 1 order by po.send_time asc,poi.create_time asc  ";
		List<Map<String, Object>> lstItems = sqlMapper.public_list_page_sql(sqllist, pagenum,pagesize);
		
		
		List<Map<String,Object>> lstMapOrder = new ArrayList<Map<String,Object>>();
		Map<String,Object> mapOrder = null;
		List<Map<String,Object>> lstMapitems = null;
		String oid = "";
		for(Map<String,Object> mapItem: lstItems)
		{
			if(oid.isEmpty() || !oid.equals(mapItem.get("oid").toString()))
			{
				mapOrder = new HashMap<String,Object>();
				oid = mapItem.get("oid").toString();
				mapOrder.put("oid", mapItem.get("oid"));
				mapOrder.put("cid", mapItem.get("cid"));
				mapOrder.put("detail_oid", mapItem.get("detail_oid"));
				mapOrder.put("contact_detail", mapItem.get("contact_detail"));
				mapOrder.put("address_detail", mapItem.get("address_detail"));
				mapOrder.put("time", mapItem.get("time"));
				//订单数据
				lstMapOrder.add(mapOrder);
				//订单里面的商品
				lstMapitems = new ArrayList<Map<String,Object>>();
				mapOrder.put("items", lstMapitems);
			}
			
			Map<String,Object> mapOrderItem = new HashMap<String,Object>(); 
			mapOrderItem.put("order_item_id", mapItem.get("order_item_id"));
			mapOrderItem.put("item_name", mapItem.get("item_name"));
			mapOrderItem.put("val_names", mapItem.get("val_names"));
			mapOrderItem.put("actual_num", mapItem.get("actual_num"));
			lstMapitems.add(mapOrderItem);
		}
		
		map.put("lstMapOrder", lstMapOrder);
		results.setResult(map);//输出：首页订单和数量信息 订单信息
		results.setMessage("查询成功");//输出：提示信息
		results.setStatus(1);//输出：状态1成功 0失败
		
	}
	
	

	//接口结束********************************************************************************************



	
}
